package com.htu.courseService.courseContents.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.htu.courseService.common.enums.ErrorCodeEnum;
import com.htu.courseService.common.exception.BusinessException;
import com.htu.courseService.courseContents.dao.DirectoriesMapper;
import com.htu.courseService.courseContents.entity.Directories;
import com.htu.courseService.courseContents.service.DirectoriesService;
import com.htu.courseService.courseContents.vo.DirectoriesDto;
import com.htu.courseService.courseManage.dao.CoursesMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class DirectoriesServiceImpl implements DirectoriesService {
    private final CoursesMapper coursesMapper;

    private final DirectoriesMapper directoriesMapper;
    @Override
    public void saveDirectories(DirectoriesDto directoriesDto) {
        String directoryName = directoriesDto.getDirectoryName();
        if(StrUtil.isEmpty(directoryName)){
            throw new BusinessException(ErrorCodeEnum.COURSE_NAME_NOT_FOUND);
        }
        Integer courseId = directoriesDto.getCourseId();
        if(null == courseId){
            throw new BusinessException(ErrorCodeEnum.COURSE_ID_NOT_FOUND);
        }
        Directories directories = BeanUtil.copyProperties(directoriesDto,Directories.class);
        Integer parentDirectoryId = directories.getParentDirectoryId();
        if(null == parentDirectoryId){
            directories.setParentDirectoryId(0);
        }
        directories.setCreatedAt(new Date());
        int count = directoriesMapper.insert(directories);
        if(count == 0){
            throw new BusinessException(ErrorCodeEnum.INSERT_FAIL);
        }
    }

    @Override
    public List<Tree<Integer>> getDirectoriesList(Integer courseId) {
        List<DirectoriesDto> nodeList = directoriesMapper.getDirectoriesList(courseId);
        List<TreeNode<Integer>> collect = nodeList.stream().map(directoriesDto -> new TreeNode<>(directoriesDto.getDirectoryId(), directoriesDto.getParentDirectoryId(), directoriesDto.getDirectoryName(), 0))
                .toList();
        // 0表示最顶层的id是0
        List<Tree<Integer>> build = TreeUtil.build(collect, 0);
        return build;
    }

    @Override
    public void deleteDirectories(Integer directoriesId) {
        int count = directoriesMapper.deleteById(directoriesId);
        if(count == 0){
            throw new BusinessException(ErrorCodeEnum.DELETE_FAIL);
        }
    }

    @Override
    public void updateDirectoriesName(DirectoriesDto directoriesDto) {
        Directories directories = BeanUtil.copyProperties(directoriesDto,Directories.class);
        int count = directoriesMapper.updateById(directories);
        if(count == 0){
            throw new BusinessException(ErrorCodeEnum.UPDATE_FAIL);
        }
    }
}
